package com.liuhouse.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.liuhouse.enums.AppHttpCodeEnum;
import com.liuhouse.pojo.Category;
import com.liuhouse.service.ICategoryService;
import com.liuhouse.utils.BeanCopyUtils;
import com.liuhouse.utils.ResponseResult;
import com.liuhouse.utils.WebUtils;
import com.liuhouse.vo.CategoryVO;
import com.liuhouse.vo.ExcelCategoryVo;
import com.liuhouse.vo.PageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//这些都是规定好的接口  所以  咱只需要遵守就行了  都是固定套路

//文章分类控制器
@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Resource
    private ICategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<CategoryVO> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }

    //权限控制 ps是PermissionService类的bean名称
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //社会下载文件的请求头,下载下来的Excel文件叫 '分类.xlsx'
            WebUtils.setDownLoadHeader("分类.xlsx" , response);
            List<Category> categoryList = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryList, ExcelCategoryVo.class);
            //进行数据写入,这是官方提供的
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response , JSON.toJSONString(result));
        }
    }



    @GetMapping("/list")
    public ResponseResult list(Category category , Integer pageNum , Integer pageSize){
        PageVo pageVo = categoryService.selectCategoryPage(category, pageNum, pageSize);
        return ResponseResult.okResult(pageVo);
    }


    @PostMapping
    public ResponseResult add(@RequestBody CategoryVO categoryVO){
        ResponseResult result = categoryService.addCategory(categoryVO);
        return result;
    }


    //删除分类
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Long id){
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }


    //获取分类数据进行回显
    @GetMapping("/{id}")
    public ResponseResult getCategory(@PathVariable(value = "id") Long id){
        //只是一条很简单的查询  所以直接使用mybatisPlus里面的方法就可以了
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }


    //进行分类的修改,传递过来的json数据 必须使用 RequestBody 注解进行标注 这是一个请求体
    @PutMapping
    public ResponseResult save(@RequestBody Category category){
        //直接使用mybatisPlus里面的方法进行修改
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }





}
